package niumao.minecraft.uhc;

import niumao.minecraft.uhc.commands.*;
import niumao.minecraft.uhc.indicator.ScoreBoardInfo;
import niumao.minecraft.uhc.listener.*;
import niumao.minecraft.uhc.recipes.UHCItem;
import niumao.minecraft.uhc.recipes.recipeGui;
import niumao.minecraft.uhc.indicator.ArrowShot;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.DisplaySlot;

public final class Main extends JavaPlugin {
    private static Main plugin;
    private static String lang = "zh_cn";

    //获取主类本身
    public static Main getInstance() {
        return plugin;
    }

    //获取主类的语言
    public static String getLang() {
        return lang;
    }

    //设置主类的语言
    public static void setLang(String lang) {
        Main.lang = lang;
    }

    //插件启动
    @Override
    public void onEnable() {
        plugin = this;
        this.getConfig().options().copyDefaults();
        this.saveDefaultConfig();

        Bukkit.getServer().getScoreboardManager().getNewScoreboard().clearSlot(DisplaySlot.SIDEBAR);

        //启动成功消息
        Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "UHC_onEnable has been invoked!");

        registerAllListener();

        setAllCommandExecutor();

        DefaultSetting.setNPCName(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".UHC"));
        DefaultSetting.setArmorStandName(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".LanguageOptions"));

        //为所有在线玩家创建右侧的计分板
        for (Player player : Bukkit.getServer().getOnlinePlayers()) {
            ScoreBoardInfo.initBase(player);
            player.setGameMode(GameMode.ADVENTURE);
            player.setInvulnerable(true);
        }
    }

    //插件关闭或重启
    @Override
    public void onDisable() {
        //关闭或重启成功消息
        Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "UHC_onDisable has been invoked!");
        Bukkit.getScoreboardManager().getNewScoreboard().clearSlot(DisplaySlot.SIDEBAR);
        DefaultSetting.setNPCName(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".UHC"));
        DefaultSetting.setArmorStandName(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".LanguageOptions"));
        Bukkit.unloadWorld(UHCGame.uhcWorld, false);
    }

    private void registerAllListener() {
        //玩家进入服务器的基本设置
        Bukkit.getServer().getPluginManager().registerEvents(new DefaultSetting(), this);
        Bukkit.getServer().getPluginManager().registerEvents(new LuckyBlockGame(), plugin);
        Bukkit.getServer().getPluginManager().registerEvents(new UHCGame(), plugin);
        Bukkit.getServer().getPluginManager().registerEvents(new Buff(), plugin);

        //添加物品配方
        new UHCItem().setRecipe();
        new UHCItem().setAllItems();
        //配方书面板
        Bukkit.getServer().getPluginManager().registerEvents(new recipeGui(), this);

        //使用弓箭射中玩家则显示该玩家剩下的血量
        Bukkit.getServer().getPluginManager().registerEvents(new ArrowShot(), this);

        //为Apprentice Sword注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new apprentice_sword(), this);
        //为Apprentice Bow注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new apprentice_artemis(), this);
        //为Golden head注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new goldenHead_Cornucopia(), this);
        //为exodus注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new exodus(), this);
        //为perun/excalibur注册监听器,并监听
        Bukkit.getServer().getPluginManager().registerEvents(new Perun_Exc(), this);
        Perun_Exc.runnablerunner();
        //监听anduril和miner
        anduril_miner.runnablerunner();
        //为miner注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new miner(), this);
        //为bloodlust注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new bloodlust(), this);
        //为modular注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new modular(), this);
        //为hermes注册监听器,并监听
        Bukkit.getServer().getPluginManager().registerEvents(new hermes(), this);
        hermes.runnablerunner();
        //为barbarian注册监听器
        barbarian.runnablerunner();
        //为聚变甲注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new fusionArmor(), this);
        //为死亡镰刀注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new scythe(), this);
        //为LumberAxe注册监听器
        Bukkit.getServer().getPluginManager().registerEvents(new TreeHandler(), this);
    }

    private void setAllCommandExecutor() {
        this.getCommand("selectlanguage").setExecutor(new SelectLanguage());
        this.getCommand("createworld").setExecutor(new Createworld());
        this.getCommand("travel").setExecutor(new Travel());
        this.getCommand("forcestart").setExecutor(new ForceStart());
        this.getCommand("forcestop").setExecutor(new ForceStop());
        this.getCommand("unloadWorld").setExecutor(new UnloadWorld());
        this.getCommand("worldList").setExecutor(new WorldList());
        this.getCommand("setPVP").setExecutor(new SetPVP());
    }
}
